/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 */

package com.device.idea.demo.core;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * CommonViewHolder
 *
 * @since 2020-12-12
 */
public class CommonViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> mViewsArray;

    private Context mContext;

    /**
     * 构造函数
     *
     * @param context context
     * @param view view
     */
    public CommonViewHolder(Context context, @NonNull View view) {
        super(view);
        mContext = context;
        mViewsArray = new SparseArray<>();
    }

    /**
     * 通过view的id获取对应的控件，如果没有则加入views中
     *
     * @param viewId 控件的id
     * @return 返回一个控件
     */
    public View getView(int viewId) {
        View view = mViewsArray.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViewsArray.put(viewId, view);
        }
        return view;
    }
}